//index.js
//获取应用实例
const app = getApp()
const config = require('../../config.js');
const weixin = {
	time: 60
}
Page({
	data: {
		isInfo:false,
		isBill:false,
		categoryIndex: '',
		billData: [],
		phone: '',
		code: '',
		name: '',
		email: '',
		qq: "",
		linkman: '',
		counselor: '',
		visible2: false,
		isShow: false,
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		setTime: '获取验证码',
	},
	//事件处理函数
	bindViewTap: function () {

	},
	onShow() {
		let userInfo = wx.getStorageSync('userInfo')
		if (userInfo === '') {
			this.setData({
				isShow: true
			})
		} else {
			this.setData({
				isShow: false,
				userInfo: userInfo
			})
			this.getUserInfoBill()
		}
	},
	isDisp() {
		this.setData({
			visible2: false,
			isShow: true
		})
	},
	getUserInfoBill: function (e) {
		let vm = this
		// console.log(e)
		// wx.showToast({
		// 	title: "暂无下载文件",
		// 	icon: 'none',
		// 	duration: 2000
		// })
		wx.request({
			url: config.downloadExampleUrl + vm.data.userInfo.id,
			data: {},
			header: {
				tenantId: 1003,
				'content-type': 'application/json', // 默认值
			},
			success(response) {

				let result = response.data
				if (result.code === "0000") {
					vm.setData({
						billData: result.data,
						isBill:true,
			             isInfo:false
					})
              
					// wx.downloadFile({
					// 	url: result.data.billUrl,
					// 	success(res) {
					// 		console.log(res)
					// 		if(res.code == '0000') {

					// 		console.log(12121)
					// 		}

					// 		// if (res.statusCode === 200) {
					// 		// 	wx.playVoice({
					// 		// 		filePath: res.tempFilePath
					// 		// 	})
					// 		// 	// wx.showToast({
					// 		// 	// 	title: "请在相册查看",
					// 		// 	// 	icon: 'none',
					// 		// 	// 	duration: 2000
					// 		// 	// })
					// 		// }
					// 	}
					// })
				} else {
					wx.showToast({
						title: result.msg,
						icon: 'none',
						duration: 2000
					})

				}
			}
		})
	},

	getUserInfoPhone: function (e) {
		this.setData({
			visible2: true
		});
	},
	handleClose2() {
		this.setData({
			visible2: false
		});
	},
	// 获取手机号码
	getPhone(e) {
		this.setData({
			phone: e.detail.value
		})
	},
	// 获取验证码
	getCodeVal(e) {
		this.setData({
			code: e.detail.value
		})
	},
	// 获取name
	getName(e) {
		this.setData({
			name: e.detail.value
		})
	},
	// 获取email
	getEmail(e) {
		this.setData({
			email: e.detail.value
		})
	},
	// 获取qq
	getQq(e) {
		this.setData({
			qq: e.detail.value
		})
	},
	// 获取联系人
	getLinkman(e) {
		this.setData({
			linkman: e.detail.value
		})
	},
	// 获取专属顾问
	getCounselor(e) {
		this.setData({
			counselor: e.detail.value
		})
	},
	// 验证手机号码
	verifyPhone(val) {
		if (!val) {
			wx.showToast({
				title: '请输入手机号码',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!/^1[3456789]\d{9}$/.test(val)) {
			wx.showToast({
				title: '请输入正确的手机号码',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		return true
	},
	// 验证邮箱
	verifyEmail(val) {
		if (!val) {
			wx.showToast({
				title: '请输入邮箱',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		if (!/^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/.test(val)) {
			wx.showToast({
				title: '请输入正确的邮箱',
				icon: 'none',
				duration: 2000
			})
			return false
		}
		return true
	},
	// 获取验证码
	getCode() {
		let index, vm = this
		if (weixin.time !== 60) {
			return
		}
		if (vm.verifyPhone(vm.data.phone)) {
			index = setInterval(() => {
				--weixin.time
				vm.setData({
					setTime: weixin.time + '秒'
				})
				if (weixin.time < 1) {
					vm.setData({
						setTime: '获取验证码'
					})
					weixin.time = 60
					clearInterval(index)
				}
			}, 1000)
			wx.request({
				url: config.verifyCode,
				data: {
					phoneNumber: vm.data.phone
				},
				header: {
					tenantId: 1003,
					'content-type': 'application/json', // 默认值
				},
				success(response) {
					// console.log(response)
				}
			})
		}
	},
	// 绑定手机号码
	bindPhone() {
		console.log(this.data.phone)
		let vm = this
		if (!this.verifyPhone(this.data.phone)) {
			return
		}
		if (!this.verifyEmail(this.data.email)) {
			return
		}
		if (!vm.data.code && !vm.data.name) {
			return wx.showToast({
				title: '请输入填写内容',
				icon: 'none',
				duration: 2000
			})
		}
		wx.request({
			url: config.bindPhone,
			data: {
				phoneNumber: vm.data.phone,
				verifyCode: vm.data.code,
				companyName: vm.data.name,
				email: vm.data.email,
				encryptedData: weixin.encryptedData,
				code: weixin.code,
				iv: weixin.iv,
				linkman: vm.data.linkman,
				counselor: vm.data.counselor,
				qq: vm.data.qq
			},
			method: "POST",
			header: {
				tenantId: 1003,
				'content-type': 'application/json', // 默认值
			},
			success(response) {
				console.log(response.data)
				let result = response.data
				if (result.code === "0000") {
					wx.showToast({
						title: '成功',
						icon: 'none',
						duration: 2000
					})
					vm.setData({
						visible2: false,
						userInfo: result.data
					});
					vm.setData({
						isShow: false,
					})
					app.globalData.userInfo = result.data
					vm.getUserInfoBill()
					vm.onLoad()
				} else {
					wx.showToast({
						title: result.msg,
						icon: 'none',
						duration: 2000
					})

				}
			}
		})
	},
	getbillupload(e) {
		// console.log(e.currentTarget.dataset.id)
		let item = e.currentTarget.dataset.id
		wx.downloadFile({
			url: item.billUrl,
			success: function (res) {
				console.log(res)
				wx.showToast({
					title: '下载成功',
					icon: 'success',
					duration: 2000
				})
				var filePath = res.tempFilePath;
				wx.openDocument({
					fileType: 'xlsx',
					filePath: filePath,
					success: function (res) {
						console.log('打开文档成功')
					}
				})
				wx.saveFile({
					filePath: filePath,
					success (res) {
						wx.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 2000
						})
					}
				})
			}
		})
	},
	// 获取授权信息
	bindGetUserInfo(e) {
		console.log("e", e)
		this.getUserLogin(e.detail)

	},
	getUserLogin(val) {
		let vm = this
		wx.login({
			success(res) {
				console.log(res)
				if (res.code) {
					weixin.encryptedData = val.encryptedData
					weixin.code = res.code
					weixin.iv = val.iv
					//发起网络请求
					wx.request({
						url: config.loginUrl,
						data: {
							code: res.code,
							encryptedData: val.encryptedData,
							iv: val.iv
						},
						header: {
							tenantId: 1003,
							'content-type': 'application/json', // 默认值
						},
						success(response) {
							console.log(response.data)
							let result = response.data
							if (!result.data) {
								vm.setData({
									visible2: true
								})
							} else {
								wx.setStorage({
									key: "userInfo",
									data: result.data,
									
								})
								vm.setData({
									userInfo: result.data,
									isShow:false
								})
								vm.getUserInfoBill()

							}
						}
					})
				} else {
					console.log('登录失败！' + res.errMsg)
				}
			},
			fail: function () {
				vm.setData({
					visible2: false
				})
			}
		})
	},
	getUserInfoData() {
		let vm = this
		vm.setData({
			isBill:false,
			isInfo:true
		})
	}
})